package ma.xin.shi.arxml.export.cherySomeip;

import org.w3c.dom.*;
//import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class CherySomeipArxmlExporter {

    private final List<CherySomeipArxmlPackage> packageList;

    public CherySomeipArxmlExporter(List<CherySomeipArxmlPackage> packageList) {
        this.packageList = packageList;
    }
    
    public void export(String outputPath) throws IOException {
        try {
            // 创建 DocumentBuilderFactory 和 DocumentBuilder
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // 创建 Document 对象
            Document doc = docBuilder.newDocument();

            // 创建根元素 <AUTOSAR>
            Element rootElement = doc.createElement("AUTOSAR");
            rootElement.setAttribute("xmlns", "http://autosar.org/schema/r4.0");
            rootElement.setAttribute("xsi:schemaLocation", "http://autosar.org/schema/r4.0 http://www.autosar.org/schema/r4.0/AUTOSAR_4-3-0.xsd");
            rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

            // ====== 强制创建 AR-PACKAGES 节点 ======
            Element arPackages = doc.createElement("AR-PACKAGES");

            if (packageList.isEmpty()) {
                // 添加注释
                Comment comment = doc.createComment(" package list start ");
                arPackages.appendChild(comment);

                // 添加一个换行缩进文本节点，保证 <AR-PACKAGES> 不是自闭合标签
                Text emptyLine = doc.createTextNode("\n");
                arPackages.appendChild(emptyLine);
            } else {
                for (CherySomeipArxmlPackage aPkg : packageList) {
                    Element pkgElement = aPkg.generateXml(doc);
                    if (pkgElement != null) {
                        arPackages.appendChild(pkgElement);
                    }
                }
            }

            // 将 AR-PACKAGES 添加到根节点
            rootElement.appendChild(arPackages);



            // 将根节点添加到文档
            doc.appendChild(rootElement);

            // 写入文件
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // 美化格式
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(outputPath));

            transformer.transform(source, result);

            System.out.println("✅ ARXML 文件已生成至：" + outputPath);

        } catch (ParserConfigurationException | TransformerException e) {
            throw new IOException("导出 ARXML 失败：" + e.getMessage(), e);
        }
    }

}
